<?php


namespace app\admin\system\controller;


use app\model\SysAdmin;
use app\model\SysRole;
use app\model\SysRoleMenu;
use think\facade\Db;

class RoleController extends BaseController
{
    protected $model = SysRole::class;

    public function list()
    {
        $roleName = input('roleName', '');
        $roleCode = input('roleCode', '');
        $roleStatus = input('roleStatus', '');
        $pageSize = intval(input('pageSize')) ?: 10;
        $pageNo = intval(input('pageNo')) ?: 1;
        $where = [];
        $roleName && $where[] = ['roleName', 'like', "%{$roleName}%"];
        $roleCode && $where[] = ['roleCode', 'like', "%{$roleCode}%"];
        $roleStatus && $where[] = ['roleStatus', '=', $roleStatus];

        $count = $this->getModel()
            ->where($where)
            ->count();

        $data = $this->getModel()
            ->where($where)
            ->limit(($pageNo - 1) * $pageSize, $pageSize)
            ->select();
        return $this->success($this->formatPage($count, $data));
    }

    public function add()
    {
        $data = request()->all();
        $this->getModel()->insert($data);
        return $this->success();
    }

    public function update()
    {
        $data = request()->all();
        $this->getModel()->where('roleId', '=', $data['roleId'])->update($data);
        return $this->success();
    }

    public function getById()
    {
        $id = input('id');
        $data = $this->getModel()->where('roleId', '=', $id)->find();
        return $this->success($data);
    }

    public function deleteById()
    {
        $id = input('id');
        if ($id) {
            $this->getModel()->where('roleId', '=', $id)->delete();
        }
        return $this->success();
    }

    public function batchDelete()
    {
        $ids = input('ids');
        if ($ids) {
            $this->getModel()->where('roleId', 'in', $ids)->delete();
        }
        return $this->success();
    }

    public function listRoleElSelect()
    {
        $data = $this->getModel()->where('status', '=', 1)->field([
            'name as label',
            'id as value',
        ])->select();
        return $this->success($data);
    }

    public function allocationMenu()
    {
        $menuIds = input('menuIds');
        $roleId = input('roleId');
        $inst = [];
        foreach ($menuIds as $id) {
            $inst[] = [
                'role_id' => $roleId,
                'menu_id' => $id,
            ];
        }
        Db::transaction(function () use ($roleId, $inst) {
            SysRoleMenu::where('role_id', '=', $roleId)->delete();
            SysRoleMenu::insertAll($inst);
        });
        return $this->success();
    }

    public function listNormalRole()
    {
        $id = input('id');

        $role_str = SysAdmin::where([
            'id' => $id
        ])->value("role_ids");
        $role_ids = [];
        if ($role_str) {
            $role_ids = explode(',', $role_str);
            $role_ids = array_map(function ($v) {
                return intval($v);
            }, $role_ids);
        }
        $data = $this->getModel()->field([
            'roleName as label',
            'roleId as value'
        ])->where('roleStatus', '=', 0)->select();
        return $this->success([
            'data1' => $data, //左边的未选择数据
            'data2' => $role_ids, //右边的已选择数据
        ]);
    }

    public function assignUserRole()
    {
        $id = input('id');
        $roleIds = input('roleIds');

        SysAdmin::where([
            'id' => $id
        ])->update([
            "role_ids" => implode(',', $roleIds)
        ]);
        return $this->success();

    }
}